import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { resolve } from 'path';
import { createStyleImportPlugin, AndDesignVueResolve } from 'vite-plugin-style-import';
import lessOption from './src/config/global.less';

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
    // 根据当前工作目录中的 `mode` 加载 .env 文件
    const env = loadEnv(mode, process.cwd());

    return {
        base: process.env.NODE_ENV === 'production' ? '/' : '/',
        plugins: [
            vue(),
            vueJsx(),
            createStyleImportPlugin({
                resolves: [AndDesignVueResolve()],
                libs: [
                    {
                        libraryName: 'ant-design-vue',
                        esModule: true,
                        resolveStyle: (name) => `ant-design-vue/es/${name}/style/index`
                    }
                ]
            })
        ],
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src')
            }
        },
        css: {
            preprocessorOptions: {
                less: lessOption
            }
        },
        server: {
            port: 8000,
            cors: true,
            origin: 'http://localhost:8000',
            proxy: {
                '/main-api': {
                    target: env.VITE_APP_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/main-api/, '/api')
                },
                '/api': {
                    target: 'http://10.141.42.21:20014',
                    changeOrigin: true,
                }
            }
        },
        build: {
            sourcemap: true,
            chunkSizeWarningLimit: 1500,
            rollupOptions: {
                output: {
                    manualChunks: {
                        'ant-design-vue': ['ant-design-vue'],
                        'vue-vendor': ['vue', 'vue-router', 'vuex']
                    }
                }
            }
        }
    };
});
